package chapter09

/**
  * 泛型
  */
object Test03_Generics {

  def main(args: Array[String]): Unit = {

    // 1.多态
    val child: Parent = new Child

    // 2.不变
    val childList: MyCollection[Parent] = new MyCollection[Parent]
    //3.协变
//    val childList2: MyCollection[Parent] = new MyCollection[Child]

    //4.逆变
    val childList3: MyCollection[SubChild] = new MyCollection[Child]

    //5.泛型上下限
    def test[A <: Child](a:A): Unit = {
      println(a.getClass.getName)
    }
    def test2[A >: Child](a:A): Unit = {
      println(a.getClass.getName)
    }
    test[Child](new SubChild) //chapter09.SubChild
    test2[Parent](new Child) //chapter09.Child
  }
}

// 定义继承关系
class Parent {}

class Child extends Parent {}

class SubChild extends Child {}


// 定义带泛型的集合类型
//协变
//class MyCollection[+E] {}
//逆变
class MyCollection[-E] {}



